<template>
  <el-card style="width: 500px">
    <el-form label-width="80px" size="small">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:9090/file/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
      >
        <img v-if="form.url" :src="form.url" class="avatar"/>
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item label="姓名">
        <el-input
          v-model="user.nickname"
          disabled
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="位置">
        <el-input
          type="textarea"
          v-model="form.location"
          autocomplete="off"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-show="!Object.keys(attendance).length" type="primary" @click="sign"
          >签到</el-button
        >
        <el-button v-show="Object.keys(attendance).length" type="primary" @click="signOut"
          >签退</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "Sign",
  data() {
    return {
      form: {},
      user: localStorage.getItem("user")
        ? JSON.parse(localStorage.getItem("user"))
        : {},
        attendance: localStorage.getItem("attendance")
        ? JSON.parse(localStorage.getItem("attendance"))
        : {},
      address: "",
    };
  },
  created() {
    this.attendance = localStorage.getItem("attendance")
      ? JSON.parse(localStorage.getItem("attendance"))
      : {};

    this.address = localStorage.getItem("address")
      ? localStorage.getItem("address")
      : "";

    this.form.location = this.address;
  },
  methods: {
    sign() {
      this.request.post("/attendance/sign", this.form).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "签到成功",
          });
          this.attendance = res.data;
          localStorage.setItem("attendance", JSON.stringify(this.attendance));
          this.form = {};
          this.form.location = this.address;
        } else {
          this.$message({
            type: "error",
            message: "签到失败",
          });
        }
      });
    },

    signOut() {
      this.form.attendanceId = this.attendance.id;
      this.request.post("/attendance/signOut", this.form).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "签退成功",
          });
          this.attendance = {};
          localStorage.removeItem("attendance");
          this.form = {};
          this.form.location = this.address;
        } else {
          this.$message({});
        }
      });
    },
    handleAvatarSuccess(res) {
      this.$set(this.form,"url",res.data) 
    },
  },
};
</script>

<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>